extends Node2D


const SPEED = 200
const SKY_SPEED = 50
var maxX = 600
var minX = 0
var knight:AnimatedSprite
onready var sky1:Sprite = $Sky1
onready var sky2:Sprite = $Sky2

# Called when the node enters the scene tree for the first time.
func _ready():
	 knight = self.get_node("Knight")
	 maxX = maxX - knight.frames.get_frame("idle",0).get_size().x /2
	 minX = minX + knight.frames.get_frame("idle",0).get_size().x /2

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
#	if $Swan.frame == 7:
#		$Swan.frame = 0
#	else:
#		$Swan.frame +=  1
		
	updateSkyAnimation(SKY_SPEED*delta)
	
	if Input.is_key_pressed(KEY_D) or Input.is_key_pressed(KEY_RIGHT):
		moveKnightX(1,SPEED,delta)	
		return
	elif Input.is_key_pressed(KEY_A) or Input.is_key_pressed(KEY_LEFT):
		moveKnightX(-1,SPEED,delta)
		return
	if knight.animation != "idle":
		knight.animation = "idle"
	
func moveKnightX(direction,speed,delta):
	if knight.animation != "run":
		knight.animation = "run"
		
	if(direction == 0):
		return;
	knight.position += Vector2(speed,0)*delta*direction;
	
	if(knight.position.x > maxX):
		knight.position = Vector2(maxX,knight.position.y)
	elif(knight.position.x < minX):
		knight.position = Vector2(minX,knight.position.y)
		
	knight.scale = Vector2(direction,1)
	
func updateSkyAnimation(speed):
	sky1.position.x = sky1.position.x - speed
	sky2.position.x = sky2.position.x - speed
	if(sky1.position.x <= -1200):
		sky1.position.x += 2400
	elif sky2.position.x <=-1200:
		sky2.position.x += 2400
	
	
